﻿using System;
using System.Net;
using System.Windows;
using Microsoft.Phone.Controls;
using PocketBlueBird.ViewModels;

namespace PocketBlueBird.Views
{
    public partial class ComposeNewTweetPage : PhoneApplicationPage
    {
        ComposeNewTweetViewModel _composeNewTweetViewModel;

        public ComposeNewTweetPage()
        {
            InitializeComponent();

            _composeNewTweetViewModel = new ComposeNewTweetViewModel();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            this.NavigationService.GoBack();
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(this._TweetContent.Text))
            {
               new Controls.FadingMessage().ShowTextMessage("No Text To Send?");
            }
            else
            {
                _composeNewTweetViewModel.SendTweet(this._TweetContent.Text, (tweet, response) =>
                    {
                        this.Dispatcher.BeginInvoke(() =>
                        {
                            if (response.StatusCode == HttpStatusCode.OK)
                            {
                                App.MyTweetsDic.Add(tweet.Id, tweet);
                                new Controls.FadingMessage().ShowTextMessage("Tweet has been send successfully.");
                                this._TweetContent.Text = string.Empty;
                            }
                            else
                            {
                                new Controls.FadingMessage().ShowTextMessage(string.Format("Send failed!error code:\r\n{0}",
                                    response.StatusCode.ToString()));
                            }
                        });
                    });
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {

        }
    }
}